<?php
App::uses('AppController', 'Controller');
/**
 * Sizes Controller
 *
 * @property Size $Size
 */
class SizesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Size->recursive = 0;
		$this->set('sizes', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Size->exists($id)) {
			throw new NotFoundException(__('Invalid size'));
		}
		$options = array('conditions' => array('Size.' . $this->Size->primaryKey => $id));
		$this->set('size', $this->Size->find('first', $options));
	}

/**
 * add method
 *
 * @return void

	public function add() {
		if ($this->request->is('post')) {
			$this->Size->create();
			if ($this->Size->save($this->request->data)) {
				$this->Session->setFlash(__('The size has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The size could not be saved. Please, try again.'));
			}
		}
	}
 */

    public function add() {
        if ($this->request->is ( 'post' )) {
            $this->Size->create ();
            // save data
            $result = $this->Size->save ( $this->request->data );
            $msg = array (
                __ ( 'The size has been saved' ),
                __ ( 'The size could not be saved. Please, try again.' )
            );
            // if ajax request, then back to json data
            if ($this->request->is ( "ajax" )) {
                header ( "Pragma: no-cache" );
                header ( "Cache-Control: no-store, no-cache, max-age=0, must-revalidate" );
                header ( 'Content-Type: text/x-json' );
                header ( 'Content-type: application/json' );
                $sizelist = array ();
                $status=false;
                if ($result) {
                    $size_all = $this->Size->find ( "all" );
                    if (! empty ( $size_all )) {
                        foreach ( $size_all as $v ) {
                            $sizelist[]=$v["Size"];
                        }
                    }
                    $status=true;
                }
                $data = array (
                    "status" => $status,
                    "message" => $status ? $msg [0] : $msg [1],
                    "sizelist" => $sizelist
                );
                echo json_encode ( $data );
                die ();
            } elseif ($result) {
                $this->Session->setFlash ( $msg [0] );
                $this->redirect ( array (
                    'action' => 'index'
                ) );
            } else {
                $this->Session->setFlash ( $msg [1] );
            }
        }
    }



/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Size->exists($id)) {
			throw new NotFoundException(__('Invalid size'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Size->save($this->request->data)) {
				$this->Session->setFlash(__('The size has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The size could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Size.' . $this->Size->primaryKey => $id));
			$this->request->data = $this->Size->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Size->id = $id;
		if (!$this->Size->exists()) {
			throw new NotFoundException(__('Invalid size'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Size->delete()) {
			$this->Session->setFlash(__('Size deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Size was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
        
        //----------------------------------------------------------------------
        
        public function beforeFilter() {
            parent::beforeFilter();
            
            $this->Auth->autoRedirect = false;
            
            $this->Auth->deny('*');   
        }
}
